/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaPesquisa.java
 *
 * Created on 24/03/2012, 08:42:11
 */

package agenda.tela;

import agenda.bean.Tabela;
import agenda.controle.ControlePesquisa;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Antonio
 */
public class TelaPesquisa extends javax.swing.JDialog {
    private String sql;
    private ControlePesquisa controle;
    private DefaultTableModel model;
    private Tabela tabela;

    /** Creates new form TelaPesquisa */
    public TelaPesquisa(java.awt.Frame parent, boolean modal, String sql) {
        super(parent, modal);
        this.sql = sql;
        controle = new ControlePesquisa();
        tabela = new Tabela();
        model = new DefaultTableModel(tabela.getLinhas(), tabela.getColunas()) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        initComponents();
    }

    public Integer getIdSelecionado() {
        int linha = jTable1.getSelectedRow();
        if (linha > -1) {
            Object obj = jTable1.getValueAt(linha, 0);
            if (obj == null) {
                return null;
            } else {
                return Integer.parseInt(obj.toString());
            }
        } else {
            return null;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        tPesquisa = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setTitle("Tela de Pesquisa");
        setMinimumSize(new java.awt.Dimension(518, 454));

        jPanel1.setPreferredSize(new java.awt.Dimension(10, 90));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Tela de Pesquisa");
        jPanel1.add(jLabel2, java.awt.BorderLayout.CENTER);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Pesquisa"));
        jPanel2.setLayout(new java.awt.BorderLayout());

        tPesquisa.setPreferredSize(new java.awt.Dimension(100, 20));
        tPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tPesquisaActionPerformed(evt);
            }
        });
        tPesquisa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tPesquisaKeyPressed(evt);
            }
        });
        jPanel2.add(tPesquisa, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel2, java.awt.BorderLayout.SOUTH);

        getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);

        jPanel3.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        jTable1.setModel(model);
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTable1KeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel3, java.awt.BorderLayout.CENTER);

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void tPesquisaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tPesquisaKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            atualiza();
        }
    }//GEN-LAST:event_tPesquisaKeyPressed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if (evt.getClickCount() > 1) {
            this.setVisible(false);
        }
    }//GEN-LAST:event_jTable1MouseClicked

    private void jTable1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            evt.consume();
            this.setVisible(false);
        }
    }//GEN-LAST:event_jTable1KeyPressed

    private void tPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tPesquisaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tPesquisaActionPerformed

    private void atualiza() {
        tabela = controle.pesquisar(sql, tPesquisa.getText());
        model.setDataVector(tabela.getLinhas(), tabela.getColunas());

        int qtde = tabela.getColunas().size();
        int[] tamanhos = new int[qtde];
        for (int i = 0; i < tamanhos.length; i++) {
            tamanhos[i] = 0;
        }
        for (Vector<Object> linha : tabela.getLinhas()) {
            for (int i = 0; i < qtde; i++) {
                int tam = linha.get(i).toString().length();
                tamanhos[i] = Math.max(tamanhos[i], tam);
            }
        }
        for (int i = 0; i < qtde; i++) {
          jTable1.getColumnModel().getColumn(i).setPreferredWidth(tamanhos[i]*10);
        }
        jTable1.updateUI();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TelaPesquisa dialog = new TelaPesquisa(new javax.swing.JFrame(), true, "");
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField tPesquisa;
    // End of variables declaration//GEN-END:variables

}
